﻿using log4net;
using SQLDataUploadHelper.daos.bases;
using SQLDataUploadHelper.models.domain;
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SQLDataUploadHelper.daos
{
    internal class UploadServerConfigDao : SqliteBaseDao
    {
        private readonly ILog log = LogManager.GetLogger(App.LogRepository.Name, typeof(UploadServerConfigDao));

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <returns></returns>
        public List<UploadServerConfig> list()
        {
            string sql = "select * from upload_server_config";
            SQLiteDataReader sqliteDataReader = SqliteDatabase.runSelectSql(sql);
            List<UploadServerConfig> list = new List<UploadServerConfig>();
            if (sqliteDataReader != null)
            {
                while (sqliteDataReader.Read())
                {
                    UploadServerConfig uploadServerConfig = new UploadServerConfig();
                    uploadServerConfig.Id = Convert.ToInt64(sqliteDataReader["id"]);
                    uploadServerConfig.Url = sqliteDataReader["url"].ToString();
                    uploadServerConfig.UserName = sqliteDataReader["user_name"].ToString();
                    uploadServerConfig.Password = sqliteDataReader["password"].ToString();
                    list.Add(uploadServerConfig);
                }
            }
            return list;
        }

        public UploadServerConfig getById(long id)
        {
            string sql = string.Format("select * from upload_server_config where id={0}", id);
            SQLiteDataReader sqliteDataReader = SqliteDatabase.runSelectSql(sql);
            UploadServerConfig uploadServerConfig = new UploadServerConfig();
            if (sqliteDataReader != null)
            {
                if (sqliteDataReader.Read())
                {
                    uploadServerConfig.Id = Convert.ToInt64(sqliteDataReader["id"]);
                    uploadServerConfig.Url = sqliteDataReader["url"].ToString();
                    uploadServerConfig.UserName = sqliteDataReader["user_name"].ToString();
                    uploadServerConfig.Password = sqliteDataReader["password"].ToString();
                }
            }
            return uploadServerConfig;
        }

        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="uploadServerConfig"></param>
        /// <returns></returns>
        public UploadServerConfig add(UploadServerConfig uploadServerConfig)
        {
            string sql = string.Format(
                "insert into " +
                "upload_server_config(url, user_name, password) " +
                "values('{0}', '{1}', '{2}')", uploadServerConfig.Url,
                uploadServerConfig.UserName, uploadServerConfig.Password);
            uploadServerConfig.Id = SqliteDatabase.execInsertSql(sql);
            return uploadServerConfig;
        }

        public bool update(UploadServerConfig uploadServerConfig)
        {
            string sql = string.Format(
                "update upload_server_config " +
                "set url='{0}', user_name='{1}', password='{2}' " +
                "where id={3} ", uploadServerConfig.Url, uploadServerConfig.UserName,
                uploadServerConfig.Password, uploadServerConfig.Id);
            return SqliteDatabase.execUpdateSql(sql);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool delete(long id)
        {
            string sql = string.Format("delete from upload_server_config where id={0}", id);
            return SqliteDatabase.execDeleteSql(sql);
        }
    }
}
